﻿using Exam.BLL;
using Exam.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ExaminationWeb.Controllers
{
    public class LibraryController : Controller
    {
        //
        // GET: /Library/
        LibraryService service = new LibraryService();
        public ActionResult Index()
        {
            return View();
        }

        public JsonResult GetAllInfos()
        {
            int pageIndex = Request["sEcho"] == null ? 0 : int.Parse(Request["sEcho"]);
            int pageSize = Request["iDisplayLength"] == null ? 15 : int.Parse(Request["iDisplayLength"]);
            int pageStart = Request["iDisplayStart"] == null ? 0 : int.Parse(Request["iDisplayStart"]);

            string SortDirindex = Request["iSortCol_0"] == null ? "" : Request["iSortCol_0"];
            string SortDir = "";
            string DataProp = "";
            if (!string.IsNullOrEmpty(SortDirindex))
            {
                DataProp = Request["mDataProp_" + SortDirindex] == null ? "" : Request["mDataProp_" + SortDirindex];
                SortDir = Request["sSortDir_0"] == null ? "" : Request["sSortDir_0"]; //asc or desc
            }

            int totalcount = 0;
            string libraryname = Request["libraryname"] == null ? "" : Request["libraryname"].Trim();
            LibraryService libraryservice = new LibraryService();
            IQueryable<Library> liblist;
            if (string.IsNullOrEmpty(libraryname))
            {
                liblist = libraryservice.LoadEntites(e => e.IsActive == 0, pageIndex, pageSize, out totalcount);
            }
            else
            {
                liblist = libraryservice.LoadEntites(e => e.LibraryName.ToLower().Contains(libraryname.ToLower()) && e.IsActive == 0, pageIndex, pageSize, out totalcount);
            }
            var results = new { iTotalDisplayRecords = totalcount, iTotalRecords = totalcount, aaData = liblist, sEcho = 0 };
            return Json(results, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Add()
        {
            return View();
        }

        public string Addinfo(Library library)
        {
            library.IsActive = 0;
            //  library.CreateTime = DateTime.Now;
            library.LibraryID = Guid.NewGuid();
            library.CreateUserID = Guid.Parse("7d372642-aa1c-4874-af01-c589793bf31a");
            DateTime time = DateTime.Now;
            library.CreateTime = time;
            library.UpdateTime = time;

            service.AddEntity(library);

            return "suc";

        }
        public ActionResult Update()
        {
            return View();
        }

    }
}
